package com.zy.cat.common.utils;

import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
/**
 * 
 * @info 
 * @author Link
 * @date 2019-11-26 13:35
 */
public class TJKUtils {
	
	public static final String YYYY_MM_DD="yyyy-MM-dd";
	
	private final static String patten="(\\\\u(\\p{XDigit}{4}))";
	
	/**
	 * 
	 * @info 时间转换为时间戳
	 * @author Link
	 * @date 2019-11-02 14:07
	 * @param date
	 * @param mate	默认：yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static long parseDate(String date, String mate) {
		if (StringUtils.isBlank(mate)) {
			mate="yyyy-MM-dd HH:mm:ss";
		}
		if(date.length()==10) {
			mate="yyyy-MM-dd";
		}
		SimpleDateFormat format = new SimpleDateFormat(mate);
		
		try {
			return format.parse(date).getTime();
		} catch (ParseException e) {
			throw new RuntimeException(e.getMessage());
		}
	}
	
	/**
	 * 
	 * @info 
	 * @author Link
	 * @date 2019-11-02 14:15
	 * @param timestamp
	 * @return
	 */
	public static LocalDateTime parse2LocalDateTime(String dateTime) {
		long timestamp=parseDate(dateTime, null);
		Instant instant = Instant.ofEpochMilli(timestamp);
		return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
	}
	
	/**
	 * 
	 * @info 
	 * @author Link
	 * @date 2019-11-02 14:15
	 * @param timestamp
	 * @return
	 */
	public static LocalDate parse2LocalDate(String dateTime) {
		LocalDateTime ldt= parse2LocalDateTime(dateTime);
		return LocalDate.from(ldt);
	}
	
	/**
	 * Unix时间戳转时间
	 * @param mate 时间格式，默认yyyy-MM-dd HH:mm:ss
	 * @param date 时间戳,毫秒
	 * @return
	 */
	public static String UnixToDateTime(String mate,long date) {
	 
		if(StringUtils.isBlank(mate)) {
			mate="yyyy-MM-dd HH:mm:ss";
		}
		return new SimpleDateFormat(mate).format(new Date(date));
	}
	/**
	 * unicode编码转换为汉字
	 * 
	 * @param unicodeStr 待转化的编码
	 * @return 返回转化后的汉子
	 */
	public static String UnicodeToCN(String unicodeStr) {
		Pattern pattern = Pattern.compile(patten);
		Matcher matcher = pattern.matcher(unicodeStr);
		char ch;
		while (matcher.find()) {
			String group = matcher.group(2);
			ch = (char) Integer.parseInt(group, 16);
			String group1 = matcher.group(1);
			unicodeStr = unicodeStr.replace(group1, ch + "");
		}
		return unicodeStr.replace("\\", "").trim();
	}

	/**
	 * 汉字转化为Unicode编码
	 * 
	 * @param CN 待转化的中文
	 * @return 返回转化之后的unicode编码
	 */
	public static String CNToUnicode(String CN) {

		try {
			StringBuffer out = new StringBuffer("");
			// 直接获取字符串的unicode二进制
			byte[] bytes = CN.getBytes("unicode");
			// 然后将其byte转换成对应的16进制表示即可
			for (int i = 0; i < bytes.length - 1; i += 2) {
				out.append("\\u");
				String str = Integer.toHexString(bytes[i + 1] & 0xff);
				for (int j = str.length(); j < 2; j++) {
					out.append("0");
				}
				String str1 = Integer.toHexString(bytes[i] & 0xff);
				out.append(str1);
				out.append(str);
			}
			return out.toString();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 
	 * @info
	 * @author Link
	 * @date 2019-10-31 17:47
	 * @param milliseconds
	 * @return
	 */
	public static String unix2LocalDateTime(long milliseconds, String pattern) {
		if (pattern == null || pattern == "") {
			pattern = "yyyy-MM-dd HH:mm:ss";
		}
		return new SimpleDateFormat(pattern).format(new Date(milliseconds));
	}

	/**
	 * 获取今日时间
	 * 
	 * @author WXL
	 * @date 2019-03-13 14:21
	 */
	public static String DateTime() {
		LocalDateTime now = LocalDateTime.now();
		return now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
	}

	/**
	 * 获取今日时间
	 * 
	 * @author WXL
	 * @date 2019-03-13 14:21
	 */
	public static String DateTimeYMD() {
		LocalDateTime now = LocalDateTime.now();
		return now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
	}
	
	/**
	 * 获取今日时间
	 * 
	 * @author WXL
	 * @date 2019-03-13 14:21
	 */
	
	/**
	 * 
	 * @info 获取今日时间 
	 * @author Link
	 * @date 2019-11-16 10:18
	 * @param pattern yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String DateTime(String pattern) {
		LocalDateTime now = LocalDateTime.now();
		return now.format(DateTimeFormatter.ofPattern(pattern));
	}

	/**
	 * 计算两个时间的秒数
	 * 
	 * @author WXL
	 * @date 2019-03-25 09:32
	 * @param stime
	 * @param etime
	 * @return
	 */
	public static long secondsInterval(String stime, String etime) {
		DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		LocalDateTime startDate = LocalDateTime.parse(stime, df);
		LocalDateTime endDate = LocalDateTime.parse(etime, df);
		return Duration.between(startDate, endDate).getSeconds();
	}

	/**
	 * 
	 * @info 把批量数据分解为N组
	 * @author Link
	 * @date 2019-07-12 13:19
	 * @param list    数据源
	 * @param perSize 每N个一组
	 * @return
	 */
	public static <T> Map<String, List<T>> groupList(List<T> list, int perSize) {
		int listSize = list.size();
		Map<String, List<T>> result = new TreeMap<String, List<T>>();
		/* 数据小于分块数据直接返回 */
		if(list.size()>=perSize) {
			/* 数据块数 */
			int keyToken = 0;
			for (int i = 0; i < list.size(); i += perSize) {
				if (i + perSize > listSize) {
					perSize = listSize - i;
				}
				result.put("" + (keyToken + 1), list.subList(i, i + perSize));
				keyToken++;
			}
		}else {
			result.put("1", list);
		}
		return result;
	}
	public static void main(String[] args) {
		System.err.println(UnixToDateTime("yyyy-MM-dd", 1572796799000L));
	}

	public static String getRandomPhone() {
		String[] ph= {"130","131","132","133","134","135","136","137","138","139","145","146","147","150","151","152","153","155","156","157","158","159","165","166","167","170","171","172","173","175","176","177","178","180","181","182","183","184","185","186","187","188","189","191","198","199"};
		int phead=RandomUtils.nextInt(0, ph.length);
		String phone=ph[phead]+"****"+RandomUtils.nextInt(1111, 9999);
	return phone;
	}
}
